package cn.edu.henu.team.ui_login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.bean._User;
import cn.edu.henu.team.constant.ModelConstant;
import cn.edu.henu.team.fragments.HomeActivity;
import cn.edu.henu.team.utils.LocationUtils;
import cn.edu.henu.team.utils.SharedPreferencesUtils;

import static cn.bmob.v3.Bmob.getApplicationContext;

public class WelcomeActivity extends AppCompatActivity {
    // 右上角的文字控件
    private AppCompatTextView countDownText;
    private CountDownTimer timer;
    protected Toast toast;

    private String addressMsg;
    private double Latitude, Longitude;

    final static String TAG = "WelcomeActivity";
    private boolean isLogin = false;  //在 checkToJump()进行逻辑判断

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // 上面全屏的代码一定要写在setContentView之前
        setContentView(R.layout.activity_welcome);

        countDownText = findViewById(R.id.tv_count_down);
        countDownText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkToJump();
            }
        });

        initCountDown();
    }

    // 倒计时逻辑
    private void initCountDown() {
        // 倒计时总时长，倒计时间隔
        timer = new CountDownTimer(1000 * 6, 1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                // 因为点击跳过会直接 finish(),所以检测一下
                if (!isFinishing()) {
                    countDownText.setText(millisUntilFinished / 1000 + " 跳过");
                }
            }
            @Override
            public void onFinish() {
                checkToJump();
            }
        }.start();
    }


    // 首次打开程序判断
    private void checkToJump() {
        boolean isFirstLogin = SharedPreferencesUtils.getBoolean(WelcomeActivity.this, ModelConstant.FIRST_LOGIN, true);
        // 首次打开，进入引导页
        if (isFirstLogin) {
            Intent it_to_guide = new Intent(WelcomeActivity.this, GuideActivity.class);
            startActivity(it_to_guide);
            SharedPreferencesUtils.putBoolean(WelcomeActivity.this, ModelConstant.FIRST_LOGIN, false);
        }
        // 非首次打开，登录回调判断
        else {
            if (isLogin) {
                LocationUtils.start(getApplicationContext(), new LocationUtils.onLocationListener() {
                    @Override
                    public void getData(AMapLocation aMapLocation) {
                        addressMsg = aMapLocation.getCity() + " " + aMapLocation.getDistrict() + " " + aMapLocation.getStreet() + " " + aMapLocation.getStreetNum();
                        Latitude = aMapLocation.getLatitude();
                        Longitude = aMapLocation.getLongitude();
                    }
                });

                final BmobGeoPoint bmobGeoPoint = new BmobGeoPoint(Longitude, Latitude);
                _User user = BmobUser.getCurrentUser(_User.class);
                user.setAddressMsg(addressMsg);
                user.setAddress(bmobGeoPoint);
                user.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                    }
                });
                startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
                finish();
            } else {
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                finish();
            }
        }
        // 回收内存
        destroyTimer();
        finish();
    }

    public void destroyTimer() {
        // 避免内存泄漏
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}